layui.config({
	base: '../static/js/'
}).extend({
	formSelects: 'formSelects-v3'
})
layui.use(['form','layer','jquery','laytpl','formSelects','treeSelect'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        laytpl = layui.laytpl,
        formSelects = layui.formSelects,
        $ = layui.jquery;
   
    function setRoleSelect(org){
    	$("#roleIds").find("option").remove();
    $.get("../role/tree", function (data) {
    	if(data){
    	data = JSON.parse(data);
    	var proHtml = '';
        for (var i = 0; i < data.length; i++) {
        	if(org == '8'){
        	if(data[i].text=='系统管理员'||data[i].text=='省级渠道经理'){
        	proHtml += '<option value="' + data[i].id + '">' + data[i].text + '</option>';
        	}
        	}else{
        		if(data[i].text=='业务员'||data[i].text=='渠道经理'){
                	proHtml += '<option value="' + data[i].id + '">' + data[i].text + '</option>';
                	}	
        	}
        }
        //初始化省数据
        $("select[name=roleIds]").append(proHtml);
        formSelects.render();
    	}else{
    		proHtml += '<option value="' + '12' + '">' + '业务员' + '</option>';
    		$("select[name=roleIds]").html(proHtml);
    		formSelects.render();
    		formSelects.value('roleIds', ['12']);
//    		formSelects.disabled('roleIds');
//          $("#roleIds").val("12");
//          alert($("#roleIds").val());
//          $("#roleIds").attr("disabled","disabled");
    	}
    });
    }
    
    var treeSelect= layui.treeSelect;
    treeSelect.render({
        // 选择器
        elem: '#organizationId',
        // 数据
        data: '../organization/tree',
        // 异步加载方式：get/post，默认get
        type: 'post',
        // 占位符
        placeholder: '修改默认提示信息',
        // 是否开启搜索功能：true/false，默认false
        search: true,
        // 点击回调
        click: function(d){
           console.log(d.current);
           $('#organizationId').val(d.current.id);
           setRoleSelect(d.current.id);
        },
        // 加载完成后的回调函数
        success: function (d) {
            console.log(d);
            treeSelect.checkNode('tree',$('#organizationId').val());
            setRoleSelect($('#organizationId').val());
        }
    });   
    
    //添加验证规则
    form.verify({
    	password : function(value, item){
            if(value.length < 6){
                return "密码长度不能小于6位";
            }
        }
    })
    
    form.on("submit(addUser)",function(data){
    	var organizationId = $("#organizationId").val();
    	var roleIds = formSelects.value('roleIds', 'valStr');;
    	var loginName = $("#loginName").val().trim();
	 	var name = $("#name").val().trim();
	 	var password = $("#password").val().trim();
	 	var age = $("#age").val().trim();
	 	var phone = $("#phone").val().trim();
    	var sex = data.field.sex;
    	var userType = data.field.userType;
    	//var roleIds = data.field.roleIds;
    	var status = data.field.status;
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
        	$.ajax({
    			type : "POST",
    			url : "../user/add",
    			data : {
    				"organizationId" : organizationId,
    				"loginName" : loginName,
    				"name" : name,
    				"password" : password,
    				"sex" : sex,
    				"userType" : userType,
    				"age" : age,
    				"roleIds" : roleIds,
    				"phone" : phone,
    				"status" : status
    			},
    			dataType : "json",
    			success : function(jsonObject) {
    				top.layer.close(index);
    	            top.layer.msg("用户添加成功！");
    	            layer.closeAll("iframe");
    	            //刷新父页面
    	            parent.location.reload();
    			}
    		});
        },2000);
        return false;
    });

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());

})